package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.WxUser;
import com.zyd.shiro.business.service.PakWxUserService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakWxUser;
import com.zyd.shiro.persistence.mapper.PakWxUserMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakWxUserServiceImpl implements PakWxUserService {

    @Autowired
    private PakWxUserMapper pakWxUserMapper;

    @Override
    public WxUser insert(WxUser wxUser) {
        Assert.notNull(wxUser, "对象不可为空！");
        wxUser.getPakWxUser().setUpdateTime(new Date());
        wxUser.getPakWxUser().setCreateTime(new Date());
        pakWxUserMapper.insertSelective(wxUser.getPakWxUser());
        return wxUser;
    }

    @Override
    public void insertList(List<WxUser> wxUsers) {
            for(WxUser wxUser:wxUsers){
               wxUser.getPakWxUser().setUpdateTime(new Date());
               wxUser.getPakWxUser().setCreateTime(new Date());
               pakWxUserMapper.insertSelective(wxUser.getPakWxUser());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakWxUserMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(WxUser wxUser) {
        Assert.notNull(wxUser, "对象不可为空！");
        wxUser.setUpdateTime(new Date());
        return pakWxUserMapper.updateByPrimaryKey(wxUser.getPakWxUser()) > 0;
    }

    @Override
    public boolean updateSelective(WxUser wxUser) {
        Assert.notNull(wxUser, "对象不可为空！");
        wxUser.setUpdateTime(new Date());
        return pakWxUserMapper.updateByPrimaryKeySelective(wxUser.getPakWxUser()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public WxUser getByPrimaryKey(Long primaryKey) {
        PakWxUser pakWxUser = pakWxUserMapper.selectByPrimaryKey(primaryKey);
        return null == pakWxUser ? null : new WxUser(pakWxUser);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public WxUser getOneByEntity(WxUser test) {
        Assert.notNull(test, "对象不可为空！");
        PakWxUser pakWxUser = pakWxUserMapper.selectOne(test.getPakWxUser());
        return null == pakWxUser ? null : new WxUser(pakWxUser);
    }

    @Override
    public List<WxUser> listAll() {
        List<PakWxUser> pakWxUsers = pakWxUserMapper.selectAll();
        if (CollectionUtils.isEmpty(pakWxUsers)) {
            return null;
        }
        List<WxUser> wxUsers = new ArrayList<>();
        for (PakWxUser pakWxUser : pakWxUsers) {
            wxUsers.add(new WxUser(pakWxUser));
        }
        return wxUsers;
    }

    @Override
    public List<WxUser> listByEntity(WxUser entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<WxUser> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakWxUser> pakWxUsers = pakWxUserMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakWxUsers)) {
            return null;
        }
        List<WxUser> wxUsers = new ArrayList<>();
        for (PakWxUser pakWxUser : pakWxUsers) {
            wxUsers.add(new WxUser(pakWxUser));
        }
        PageInfo bean = new PageInfo<PakWxUser>(pakWxUsers);
        bean.setList(wxUsers);
        return bean;
    }
    @Override
    public void increase(PakWxUser pakWxUser) {
        pakWxUser.setUpdateTime(new Date());
        pakWxUser.setCreateTime(new Date());
        pakWxUserMapper.insert(pakWxUser);
    }


    @Override
    public PakWxUser findByOpenid(String openid) {
        return pakWxUserMapper.findByOpenid(openid);
    }

    @Override
    public PakWxUser findByUsername(String username) {
        return pakWxUserMapper.findByUsername(username);
    }

}
